/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SessionBeans;

import Classes.Project;
import Classes.User;
import javax.ejb.Stateful;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author paly
 */
@Stateful
public class UserSessionBean implements UserSessionLocal {
    private User user;
    private Project project;
    
    public void initialize(User user) {
        this.user = user;
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.setAttribute("user", this.user);
    }

    public void deinitialize() {
        this.user = null;
        this.project = null;
    }

    public void enterProject(Project project) {
        if (this.user != null) {
            this.project = project;
        }
    }
    
    public Project showCurrentProject(){
        return this.project;
    }

    public void leaveProject() {
        if (this.user != null) {
            this.project = null;
        }
    }

    public User getUser() {
        return this.user;
    }

    public int getUserId() {
        if (user != null) {
            return user.getUserId();
        }
        else {
            return 0;
        }
    }
    
    public String getUserName() {
        if (user != null) {
            return user.getUserName();
        }
        else {
            return "";
        }
    }
}
